package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpDaibiaoMapper;
import com.ruoyi.system.domain.SpDaibiao;
import com.ruoyi.system.service.ISpDaibiaoService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 代队Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
public class SpDaibiaoServiceImpl implements ISpDaibiaoService 
{
    @Autowired
    private SpDaibiaoMapper spDaibiaoMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询代队
     * 
     * @param dbId 代队主键
     * @return 代队
     */
    @Override
    public SpDaibiao selectSpDaibiaoByDbId(Long dbId)
    {
        return spDaibiaoMapper.selectSpDaibiaoByDbId(dbId);
    }

    /**
     * 查询代队列表
     * 
     * @param spDaibiao 代队
     * @return 代队
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpDaibiao> selectSpDaibiaoList(SpDaibiao spDaibiao)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spDaibiao.setDatascope(datascope);
        if(datascope==4){
            spDaibiao.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spDaibiao.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spDaibiao.setCheckHuiyuanId(0l);
            }

        }
        return spDaibiaoMapper.selectSpDaibiaoList(spDaibiao);
    }

    @Override
    public List<SpDaibiao> selectSpDaibiaoListWeb(SpDaibiao spDaibiao)
    {
        return spDaibiaoMapper.selectSpDaibiaoList(spDaibiao);
    }

    /**
     * 新增代队
     * 
     * @param spDaibiao 代队
     * @return 结果
     */
    @Override
    public int insertSpDaibiao(SpDaibiao spDaibiao)
    {
        spDaibiao.setCreateTime(DateUtils.getNowDate());
        return spDaibiaoMapper.insertSpDaibiao(spDaibiao);
    }

    /**
     * 修改代队
     * 
     * @param spDaibiao 代队
     * @return 结果
     */
    @Override
    public int updateSpDaibiao(SpDaibiao spDaibiao)
    {
        return spDaibiaoMapper.updateSpDaibiao(spDaibiao);
    }

    /**
     * 批量删除代队
     * 
     * @param dbIds 需要删除的代队主键
     * @return 结果
     */
    @Override
    public int deleteSpDaibiaoByDbIds(Long[] dbIds)
    {
        return spDaibiaoMapper.deleteSpDaibiaoByDbIds(dbIds);
    }

    /**
     * 删除代队信息
     * 
     * @param dbId 代队主键
     * @return 结果
     */
    @Override
    public int deleteSpDaibiaoByDbId(Long dbId)
    {
        return spDaibiaoMapper.deleteSpDaibiaoByDbId(dbId);
    }
}
